package com.gege;

import java.util.concurrent.CompletableFuture;

/**
 * @author gechenpeng
 * @date 2021-04-12
 */
public class Demo4 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        String message = CompletableFuture.supplyAsync(() -> {
            try {
                return execute();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            return "";
        }).join();

        System.out.println("线程运行的结果是: " + message + ", 耗时: " +
                (System.currentTimeMillis() - start) + " ms");
    }

    private static String execute() throws Exception {
        Thread.sleep(5000);
        return "ok";
    }
}
